﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Assignment1
{
    class LoadMap
    {
        #region  Public Properties
        public static MapInfo globalMap;
        #endregion

        #region Properties
        private string fileName;
        /// <summary>
        /// File name of the loading map
        /// </summary>
        public string FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                fileName = value;
            }
        }

        #endregion

        #region Constructor
        public LoadMap(string aFileName)
        {
            fileName = aFileName;
            LoadMap.globalMap = new MapInfo();
            ReadData();
        }
        #endregion

        #region Private methods
        private void ReadData()
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read)))
            {
                while (!reader.EndOfStream)
                {
                    string currentLine = reader.ReadLine();
                    List<BlockType> tmp = new List<BlockType>();
                    for (int charCount = 0; charCount < currentLine.Length; charCount++)
                    {
                        switch (currentLine[charCount])
                        {
                            case 'X':
                                tmp.Add(BlockType.WALL);
                                break;
                            case 'S':
                                tmp.Add(BlockType.START);
                                LoadMap.globalMap.StartPos = new Position(charCount, LoadMap.globalMap.MapData.Count);
                                break;
                            case 'G':
                                tmp.Add(BlockType.GOAL);
                                LoadMap.globalMap.GoalSpot = new Position(charCount, LoadMap.globalMap.MapData.Count);
                                break;
                            default:
                                tmp.Add(BlockType.TERRAIN);
                                break;
                        }
                    }
                    LoadMap.globalMap.addRow(tmp);
                    LoadMap.globalMap.MaxWidth = Math.Max(LoadMap.globalMap.MaxWidth, tmp.Count);
                }
                LoadMap.globalMap.MaxHeight = LoadMap.globalMap.MapData.Count;
            }
        }
        #endregion
    }
}
